package com.mcxx.modules.base.utils;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.miniSalvation.fundpay.dao.read.FundPayMemberReadDao;
import com.mcxx.modules.miniSalvation.fundpay.dto.StandFundPayDTO;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundPayFamilyEntity;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundPayMemberEntity;
import com.mcxx.modules.miniSalvation.order.entity.OrderCertEntity;
import com.mcxx.modules.statistics.dto.PovertyApprovalDTO;
import com.mcxx.util.Constant;
import org.springframework.beans.BeanUtils;

import java.util.*;
import java.util.stream.Collectors;


public class FundPayBizDealUtils {


    public static List<FundPayFamilyEntity> reGenerateFamilyInfo(List<FundPayFamilyEntity> fundPayFamilyList, List<FundPayMemberEntity> fundPayMemberList) {
        Map<String, List<FundPayMemberEntity>> familyMap = fundPayMemberList.stream().collect(Collectors.groupingBy(FundPayMemberEntity::getFamilyId));
        Set<String> keySet = familyMap.keySet();
        List<FundPayFamilyEntity> temp = new ArrayList<>();
        for (String key : keySet) {
            Optional<FundPayFamilyEntity> curFamilyDto = fundPayFamilyList.stream().filter(e -> e.getFamilyId().equals(key)).findFirst();
            FundPayFamilyEntity curFamily = null;
            if (curFamilyDto.isPresent())
                curFamily = curFamilyDto.get();

            List<FundPayMemberEntity> members = familyMap.get(key);
            curFamily.setPersonNumber(members.size());

            Double amount = members.stream().mapToDouble(e -> null == e.getFundNumber() ? 0d : e.getFundNumber()).sum();
            curFamily.setFundNumber(amount);
            temp.add(curFamily);
        }
        return temp;
    }


    public static void dealWithPauseMember(StandFundPayDTO param,
                                           List<String> orderIdList,
                                           List<FundPayMemberEntity> fundPayMemberEntityList,
                                           FundPayMemberReadDao mapperDao) {
        List<FundPayMemberEntity> removeMember = new ArrayList<>();
        if (Constant.CapitalFundPayCostType.NURSING.equals(param.getCostType()) && Constant.SupplyType.SUPPLY_FOCUS.equals(param.getSupplyType())) {
            List<FundPayMemberEntity> suspend = mapperDao.getCareFeeSuspendPersonInfo(orderIdList);
            if (!suspend.isEmpty()) {
                for (FundPayMemberEntity obj : fundPayMemberEntityList) {
                    for (FundPayMemberEntity suspendMember : suspend) {
                        if (obj.getMemberId().equals(suspendMember.getMemberId())) {
                            removeMember.add(obj);
                        }
                    }
                }
            }
        }
        fundPayMemberEntityList.removeAll(removeMember);
    }



    public static void dealWithCarePersonInfo(StandFundPayDTO param, List<String> orderIdList, List<FundPayMemberEntity> fundPayMemberEntityList, FundPayMemberReadDao mapperDao) {
        List<FundPayMemberEntity> conditionPersons = mapperDao.getCarePersonInfo(orderIdList, param.getCareType(),param.getPayDate());
        for (FundPayMemberEntity obj: fundPayMemberEntityList) {
            for (FundPayMemberEntity careObj: conditionPersons) {
                if (obj.getOrderId().equals(careObj.getOrderId()) && obj.getMemberId().equals(careObj.getMemberId())) {
                    obj.setCareId(careObj.getCareId());
                }
            }
        }
    }


    public static List<FundPayFamilyEntity> generatorFamilyByMemberInfo(List<FundPayMemberEntity> rollMembers, StandFundPayDTO standFundPayDTO, List<FundPayFamilyEntity> fundPayFamilyList) {
        List<FundPayFamilyEntity> results = new ArrayList<>();
        Map<String, List<FundPayMemberEntity>> familyMemberMap = rollMembers.stream().collect(Collectors.groupingBy(FundPayMemberEntity::getFamilyId));
        Set<String> keySet = familyMemberMap.keySet();
        Iterator<String> it = keySet.iterator();
        while (it.hasNext()) {
            String key = it.next();

            Optional<FundPayFamilyEntity> familyDTO = fundPayFamilyList.stream().filter(e -> e.getFamilyId().equals(key)).findFirst();
            if (!familyDTO.isPresent()) {
                throw new BizException("-1", "家庭信息为空");
            }
            FundPayFamilyEntity familyOld = familyDTO.get();
            List<FundPayMemberEntity> familyMembers = familyMemberMap.get(key);
            FundPayFamilyEntity family = new FundPayFamilyEntity();
            family.setFamilyId(familyOld.getFamilyId());
            family.setId(Constant.getUUID());
            family.setAreaCode(familyOld.getAreaCode());
            family.setMemberId(familyOld.getMemberId());
            family.setOrderId(familyOld.getOrderId());
            family.setFundPayId(standFundPayDTO.getId());
            family.setPayDate(familyOld.getPayDate());
            family.setRemark(familyOld.getRemark());
            family.setCountyCode(familyOld.getCountyCode());
            family.setStreetCode(familyOld.getStreetCode());
            Double amount = familyMembers.stream().mapToDouble(e -> null == e.getFundNumber() ? 0d : e.getFundNumber()).sum();
            family.setFundNumber(amount);
            family.setPersonNumber(familyMembers.size());
            results.add(family);
        }
        return results;
    }
}
